using StatsBase,Statistics,Plots,Distributions
plotlyjs();
p = 0.5
n = 10
s = zeros(n+1);
function path(n)
x = zeros(n)
for i in 1:n
if rand(Binomial(1,p)) == 1
x[i] = 1
else
x[i] = -1
end
end
return x
end
path (generic function with 1 method)
x = path(10);
function cumulative_sum(x)
s = zeros(length(x)+1)
for i in 1:length(x)
s[i+1] = sum(x[j] for j in 1:i)
end
return s
end
cumulative_sum (generic function with 1 method)
cumulative_sum(x)
11-element Vector{Float64}:
0.0
-1.0
0.0
-1.0
0.0
1.0
2.0
1.0
0.0
-1.0
0.0
s[2:n+1] = cumsum(x) # this is inbuilt function
10-element Vector{Float64}:
-1.0
0.0
-1.0
0.0
1.0
2.0
1.0
0.0
-1.0
0.0
rep = 10000
smat = Matrix{Float32}(undef,rep,n+1); # define an empty matrix
smat[:,:1] = zeros(rep); # put zeros in the first column of the matrix
smat[:,:1] = zeros(rep);
x = zeros(n)
for k in 1:rep
for i in 1:n
if rand(Binomial(1,p)) == 1
x[i] = 1
else
x[i] = -1
end
smat[k,2:n+1] = cumsum(x)
end
end
smat
10000×11 Matrix{Float32}:
0.0 -1.0 -2.0 -1.0 -2.0 -1.0 0.0 1.0 2.0 3.0 2.0
0.0 1.0 2.0 1.0 0.0 -1.0 -2.0 -1.0 0.0 -1.0 -2.0
0.0 1.0 0.0 -1.0 0.0 -1.0 -2.0 -3.0 -2.0 -1.0 0.0
0.0 1.0 2.0 3.0 4.0 3.0 2.0 3.0 2.0 1.0 2.0
0.0 1.0 0.0 -1.0 0.0 1.0 0.0 1.0 2.0 3.0 2.0
0.0 -1.0 -2.0 -3.0 -4.0 -5.0 -6.0 -5.0 -6.0 -5.0 -6.0
0.0 -1.0 0.0 1.0 2.0 3.0 4.0 3.0 4.0 3.0 2.0
0.0 -1.0 -2.0 -3.0 -2.0 -1.0 -2.0 -1.0 0.0 1.0 2.0
0.0 1.0 2.0 3.0 4.0 3.0 4.0 3.0 4.0 3.0 4.0
0.0 -1.0 -2.0 -3.0 -4.0 -3.0 -2.0 -3.0 -4.0 -3.0 -4.0
0.0 -1.0 -2.0 -3.0 -2.0 -3.0 -2.0 -3.0 -4.0 -5.0 -4.0
0.0 -1.0 0.0 1.0 2.0 3.0 4.0 3.0 2.0 1.0 2.0
0.0 -1.0 0.0 1.0 0.0 -1.0 0.0 -1.0 -2.0 -1.0 0.0
⋮ ⋮ ⋮
0.0 1.0 0.0 1.0 2.0 1.0 0.0 1.0 2.0 3.0 2.0
0.0 -1.0 0.0 -1.0 0.0 1.0 0.0 1.0 2.0 3.0 4.0
0.0 1.0 0.0 -1.0 -2.0 -1.0 -2.0 -3.0 -4.0 -3.0 -4.0
0.0 1.0 2.0 1.0 0.0 -1.0 -2.0 -3.0 -2.0 -1.0 -2.0
0.0 -1.0 0.0 -1.0 0.0 1.0 2.0 1.0 2.0 3.0 2.0
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 6.0 7.0 8.0
0.0 -1.0 -2.0 -3.0 -4.0 -3.0 -4.0 -3.0 -4.0 -3.0 -2.0
0.0 1.0 0.0 -1.0 0.0 1.0 2.0 3.0 2.0 1.0 2.0
0.0 1.0 0.0 -1.0 -2.0 -3.0 -4.0 -5.0 -4.0 -3.0 -4.0
0.0 -1.0 0.0 1.0 2.0 3.0 4.0 3.0 4.0 5.0 6.0
0.0 1.0 2.0 1.0 0.0 1.0 0.0 -1.0 0.0 -1.0 -2.0
0.0 1.0 2.0 1.0 0.0 1.0 0.0 1.0 2.0 1.0 0.0
In the below code we have plotted the matrix smat where each row a random walk paths and we have do this work 10000 times and every time we have started from the origin. So, if we take number of steps is even then we end our random walk in an odd position as every time started from 0. If we take the number of steps is odd then we end our random walk in an even position. This matrix plot describes this.
plot(smat',label = false)
scatter!(repeat([n+1], 2n+1), -n:n,
markershape = :circle, markercolor = "red")
plot!(legend =false)
count_occur = []
for j in -n:2:n
push!(count_occur,count(i -> (i == j),smat[:,11]))
end
println(count_occur)
Any[13, 96, 469, 1164, 1997, 2427, 2130, 1142, 455, 93, 14]
probability = count_occur/rep
11-element Vector{Float64}:
0.0013
0.0096
0.0469
0.1164
0.1997
0.2427
0.213
0.1142
0.0455
0.0093
0.0014
Below plot is actually is step function.
plot(range(start = -n,stop =n,step = 2),probability)
scatter!(range(start = -n,stop =n,step = 2),probability)
plot!(legend = false)